import os
import shutil


class GetFile(object):

    def __init__(self):
        self.basepath = r"E:\样例数据\isbn_cover\book_cover_20190826"
        self.basepath2 = r"E:\样例数据\isbn_cover\book_cover_20190829"
        self.path1 = ""
        self.path2 = ""
        self.sets = set()

    def get(self):
        with open("./difference.txt", 'r', encoding="utf-8") as f:
            for fLine in f:
                filename = fLine.strip()
                if len(filename) == 10:
                    self.path1 = "isbn10"
                    self.path2 = filename[:6]
                    filename = filename + ".jpg"
                else:
                    self.path1 = "isbn13"
                    self.path2 = filename[:9]
                    filename = filename + ".jpg"
                path = os.path.join(self.basepath, self.path1, self.path2, filename)
                print(path)
                if os.path.exists(path):
                    desdir = os.path.join(self.basepath2, self.path1, self.path2)
                    if not os.path.exists(desdir):
                        os.makedirs(desdir)
                    desfilepath = os.path.join(self.basepath2, self.path1, self.path2, filename)
                    shutil.copy(path, desfilepath)
                else:
                    self.sets.add(fLine)

    def writenotfound(self):
        with open("./notfond.txt", 'w', encoding="utf-8") as f:
            for value in self.sets:
                f.write(value + "\n")



if __name__ == "__main__":
    get = GetFile()
    get.get()
    get.writenotfound()